package com.alvin.demo.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Tcp_Utils_Bio {

    public String sendCommand(String host,String post,String message){
        StringBuilder result = new StringBuilder();
        Socket socket ;
        try {
            socket = new Socket(host, Integer.parseInt(post));
        }catch (IOException e){
            return "error";
        }
        try (
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
            // 发送请求
            out.println(message);
            // 接收响应
            String response;
            while ((response = in.readLine()) != null) {
                result.append(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result.toString();
    }

    public static void main(String[] args) {
        Tcp_Utils_Bio tcp_utils_bio = new Tcp_Utils_Bio() ;
        tcp_utils_bio.sendCommand("192.168.0.102", "8887" , "");
    }
}
